package com.hk.service.business.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hk.domain.dto.DataSyncDTO;
import com.hk.domain.entity.DataStatesTask;
import com.hk.enums.SyncStatusEnum;
import com.hk.service.demo.DataStatesTaskService;
import com.hk.utils.R;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

@Slf4j
@Service
public class DataSyncBusiness {

    @Autowired
    private DataStatesTaskService taskService;


    public R insertData(DataSyncDTO dto) {
        DataStatesTask statesTask = new DataStatesTask();
        BeanUtils.copyProperties(dto, statesTask);
        statesTask.setStatus(SyncStatusEnum.NOT_SYNC.getCode());
        statesTask.setSjjcsj(new Date());
        boolean save = taskService.save(statesTask);
        return save ? R.ok() : R.error();
    }

    /**
     * 定时修改状态，每6秒执行一次
     */
//    @Scheduled(cron = "0/6 * * * * ?")
//    @SchedulerLock(name = "TaskScheduler_syncData", lockAtLeastForString = "PT1M", lockAtMostForString = "PT1M")
    public void TaskScheduler_syncData() {
        log.info("================DataSyncBusiness.TaskScheduler_syncData 任务开始================");
        //模拟查到数据了，直接改状态
        LambdaQueryWrapper<DataStatesTask> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(DataStatesTask::getTaxType, "");
//        lambdaQueryWrapper.eq(DataStatesTask::getPeriod, "");
        lambdaQueryWrapper.in(DataStatesTask::getStatus, Arrays.asList(SyncStatusEnum.NOT_SYNC.getCode()));

        DataStatesTask updateTask = new DataStatesTask();
        updateTask.setStatus(SyncStatusEnum.ALREADY_SYNC.getCode());
        updateTask.setSjjcsj(new Date());
        taskService.update(updateTask, lambdaQueryWrapper);
        log.info("================DataSyncBusiness.TaskScheduler_syncData 任务结束================");
    }





}
